package cn.dugcrs.basic.controller;

import cn.dugcrs.basic.dto.LoginDto;
import cn.dugcrs.basic.dto.WechatDto;
import cn.dugcrs.basic.exception.BusinessException;
import cn.dugcrs.basic.service.ILoginService;
import cn.dugcrs.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;


    /**
     * 手机号绑定
     * @param dto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatDto dto){
        try {
            Map<String, Object> account = loginService.binder(dto);
            return AjaxResult.success().setResultObj(account);
        } catch (BusinessException e) {     // 自定义异常，处理我们业务层抛出的错误信息
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     *用户扫码登录
     * @param dto
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDto dto){
        try {
            return loginService.wechat(dto);
        } catch (BusinessException e) {     // 自定义异常，处理我们业务层抛出的错误信息
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     *用户账号密码登录
     * @param dto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> account = loginService.account(dto);
            return AjaxResult.success().setResultObj(account);
        } catch (BusinessException e) {     // 自定义异常，处理我们业务层抛出的错误信息
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


}
